home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / Mesa-1.2.1 / contrib / gle.2.1 / demo / basic / cylinder.c next >
Encoding:
C/C++ Source or Header  |  1995-07-05  |  3.0 KB  |  133 lines

  1.  
  2. /* cylinder drawing demo */
  3.  
  4. /* required include files */
  5. #include <GL/gl.h>
  6. #include "glut.h"
  7. #include "GL/tube.h"
  8.  
  9. /* the arrays in which we will store out polyline */
  10. #define NPTS 6
  11. double points [NPTS][3];
  12. float colors [NPTS][3];
  13. int idx = 0;
  14.  
  15. /* some utilities for filling that array */
  16. #define PNT(x,y,z) {             \
  17.    points[idx][0] = x;             \
  18.    points[idx][1] = y;             \
  19.    points[idx][2] = z;            \
  20.    idx ++;                \
  21. }
  22.  
  23. #define COL(r,g,b) {             \
  24.    colors[idx][0] = r;             \
  25.    colors[idx][1] = g;             \
  26.    colors[idx][2] = b;            \
  27. }
  28.  
  29. /* 
  30.  * Initialize a bent shape with three segments. 
  31.  * The data format is a polyline.
  32.  *
  33.  * NOTE that neither the first, nor the last segment are drawn.
  34.  * The first & last segment serve only to determine that angle 
  35.  * at which the endcaps are drawn.
  36.  */
  37.  
  38. void InitCyl (void) {
  39.  
  40.    COL (0.0, 0.0, 0.0);
  41.    PNT (-6.0, 6.0, 0.0);
  42.  
  43.    COL (0.0, 0.8, 0.3);
  44.    PNT (6.0, 6.0, 0.0);
  45.  
  46.    COL (0.8, 0.3, 0.0);
  47.    PNT (6.0, -6.0, 0.0);
  48.  
  49.    COL (0.2, 0.3, 0.9);
  50.    PNT (-6.0, -6.0, 0.0);
  51.  
  52.    COL (0.2, 0.8, 0.5);
  53.    PNT (-6.0, 6.0, 0.0);
  54.  
  55.    COL (0.0, 0.0, 0.0);
  56.    PNT (6.0, 6.0, 0.0);
  57. }
  58.  
  59. float lastx=0;
  60. float lasty=0;
  61.  
  62. /* draw the cylinder shape */
  63. void DrawCyl (void) {
  64.  
  65.    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  66.  
  67.    /* set up some matrices so that the object spins with the mouse */
  68.    glPushMatrix ();
  69.    glTranslatef (0.0, 0.0, -80.0);
  70.    glRotatef (lastx, 0.0, 1.0, 0.0);
  71.    glRotatef (lasty, 1.0, 0.0, 0.0);
  72.  
  73.    /* Phew. FINALLY, Draw the polycylinder  -- */
  74.    gleSetJoinStyle (TUBE_NORM_EDGE | TUBE_JN_ANGLE | TUBE_JN_CAP);
  75.    glePolyCylinder (NPTS, points, colors, 2.3);
  76.  
  77.    glPopMatrix ();
  78.  
  79.    glutSwapBuffers ();
  80. }
  81.  
  82. /* get notified of mouse motions */
  83. void MouseMotion (int x, int y)
  84. {
  85.    lastx = x;
  86.    lasty = y;
  87.    glutPostRedisplay ();
  88. }
  89.  
  90.  
  91. /* set up a light */
  92. GLfloat lightOnePosition[] = {40.0, 40, 100.0, 0.0};
  93. GLfloat lightOneColor[] = {0.99, 0.99, 0.99, 1.0}; 
  94.  
  95. GLfloat lightTwoPosition[] = {-40.0, 40, 100.0, 0.0};
  96. GLfloat lightTwoColor[] = {0.99, 0.99, 0.99, 1.0}; 
  97.  
  98. main (int argc, char * argv[]) {
  99.  
  100.    /* initialize glut */
  101.    glutInit (&argc, argv);
  102.    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  103.    glutCreateWindow ("cylinder");
  104.    glutDisplayFunc (DrawCyl);
  105.    glutMotionFunc (MouseMotion);
  106.  
  107.    /* initialize GL */
  108.    glClearDepth (1.0);
  109.    glEnable (GL_DEPTH_TEST);
  110.    glClearColor (0.0, 0.0, 0.0, 0.0);
  111.    glShadeModel (GL_SMOOTH);
  112.  
  113.    glMatrixMode (GL_PROJECTION);
  114.    /* roughly, measured in centimeters */
  115.    glFrustum (-9.0, 9.0, -9.0, 9.0, 50.0, 150.0);
  116.    glMatrixMode(GL_MODELVIEW);
  117.  
  118.    /* initialize lighting */
  119.    glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition);
  120.    glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor);
  121.    glEnable (GL_LIGHT0);
  122.    glLightfv (GL_LIGHT1, GL_POSITION, lightTwoPosition);
  123.    glLightfv (GL_LIGHT1, GL_DIFFUSE, lightTwoColor);
  124.    glEnable (GL_LIGHT1);
  125.    glEnable (GL_LIGHTING);
  126.    glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
  127.    glEnable (GL_COLOR_MATERIAL);
  128.  
  129.    InitCyl ();
  130.  
  131.    glutMainLoop ();
  132. }
  133.